package com.kayt.cyberBar.cuser.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.GenPublicCode;
import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.common.utils.TimeUtils;
import com.kayt.cyberBar.cuser.business.dao.ISheetStoreWorkDao;
import com.kayt.cyberBar.cuser.business.entity.po.BSheetStoreWork;

@Repository("sheetStoreWorkDao")
public class SheetStoreWorkDaoImpl implements ISheetStoreWorkDao {
	@Autowired
	private EntityDao entityDao;
	@Override
	public Integer save(BSheetStoreWork entity) throws SQLException {
			if(entity != null){
				entityDao.save(entity);
				return 0;
			}else{
				return -1;
			}
	}

	@Override
	public Integer update(BSheetStoreWork entity) throws SQLException {
		
			if(entity != null){
				entityDao.update(entity);
				return 0;
			}else{
				return -1;
			}
	}

	@Override
	public Integer delete(Integer id) {
		
			if(id != null){
				BSheetStoreWork BSheetStoreWork = findById(id);
				if(BSheetStoreWork != null){
					entityDao.delete(BSheetStoreWork);
					return 0;
				}else{
					return -1;
				}
			}else{
				return -1;
			}
	}

	@Override
	public BSheetStoreWork findById(Integer id) {
		
			if(id != null){
				return entityDao.findById(id, BSheetStoreWork.class);
			}else{
				return null;
			}
	}

	@Override
	public PageData<BSheetStoreWork> findByPage(int pageNum, int pageSize, String hql, Object[] params,
			LinkedHashMap<String, String> orderMap) throws SQLException {
		return this.entityDao.getPagingData(BSheetStoreWork.class, pageNum, pageSize, hql, params, orderMap);
	}

	@Override
	public BSheetStoreWork findBySotreCode(String storeCode) throws SQLException {
		String sql = "select * from b_sheet_store_work where STORE_CODE = '"+storeCode+"'";
		List<BSheetStoreWork> storeWorkList = entityDao.getPagingDataBySql(BSheetStoreWork.class, -1, -1, sql, null);
		if(storeWorkList != null && storeWorkList.size() != 0){
			return storeWorkList.get(0);
		}else{
			BSheetStoreWork sheetStoreWork = new BSheetStoreWork();
			sheetStoreWork.setCreateTime(TimeUtils.generateTimeStr());
			sheetStoreWork.setUpdateTime(TimeUtils.generateTimeStr());
			sheetStoreWork.setWorkCode(GenPublicCode.genSheetCode());
			sheetStoreWork.setStoreCode(storeCode);
			entityDao.save(sheetStoreWork);
			return sheetStoreWork;
		}
	}
}
